/*******************************************************************************

    copyright:  Copyright (c) 2007 Matthias Walter. All rights reserved

    license:    BSD style: $(LICENSE)

    version:    Initial release: May 2007

    author:     Matthias Walter

*******************************************************************************/

module fcgi.InternalConduit;

private import  tango.io.device.Conduit;

private import  fcgi.Base;

class FastCGIInternalConduit : Conduit
{
    private int fileDescriptor_ = -1;
    
    public void init (int fd)
    {
        fileDescriptor_ = fd;
    }
    
    public void finish ()
    {
        fileDescriptor_ = -1;
    }
    
    size_t read (void[] dst)
    {
        if (fileDescriptor_ < 0)
            return Eof;

        int rd = OS_Read (fileDescriptor_, dst.ptr, dst.length);

        if (rd <= 0)
            return Eof;
        else
            return rd;
    }

    size_t write (void[] dst) 
    {
        if (fileDescriptor_ < 0)
            return Eof;
        int written = OS_Write (fileDescriptor_, dst.ptr, dst.length);
        if (written < 0)
            return Eof;
        else
            return written;
    }

    void disconnect ()
    {

    }
    
    size_t bufferSize ()
    {
        return 1024;
    }

    /*
    Handle fileHandle()
    {
        return cast(Handle) fileDescriptor_;
    }
    */

    void detach ()
    {

    }

    char[] toString ()
    {
        return "FastCGIInternalConduit";
    }
}
